package com.qigao.mall.web.controller.app.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.RefundService;
import com.qigao.mall.commons.dto.RefundAuditDto;
import com.qigao.mall.commons.dto.RefundOrderResultDto;
import com.qigao.mall.commons.dto.RefundQueryDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.RefundAuditVo;
import com.qigao.mall.web.vo.app.RefundQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/merchant/refund")
@Api(tags = "App商户退款相关API",value = "App商户退款相关API")
public class MerchantRefundController {
    private static Logger logger= LoggerFactory.getLogger(MerchantRefundController.class);
    @Resource
    RefundService refundService;

    //1.退款申请
    @ApiOperation("退换货审核")
    @PostMapping("/audit")
    public Result refundAudit(@RequestBody RefundAuditVo refundAuditVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(refundAuditVo);
        try{
            RefundAuditDto refundAuditDto=new RefundAuditDto();
            BeanUtils.copyProperties(refundAuditVo,refundAuditDto);
            refundAuditDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            return refundService.refundAudit(refundAuditDto);
        }catch (BusinessException be){
            logger.error("退换货审核异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("退换货审核发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //2.查询退款单列表
    @GetMapping("/findRefundList")
    public Result<PageInfo<RefundOrderResultDto>> findRefundList(RefundQueryVo refundQueryVo){
        ValidatorUtil.validateEntity(refundQueryVo);
        try{
            RefundQueryDto refundQueryDto=new RefundQueryDto();
            BeanUtils.copyProperties(refundQueryVo,refundQueryDto);
            return refundService.findRefundList(refundQueryDto);
        }catch (Exception e){
            logger.error("按查询退款单列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


}
